home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / bind493a.zip / named / db_glue.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  35KB  |  1,225 lines

  1. #if !defined(lint) && !defined(SABER)
  2. static char sccsid[] = "@(#)db_glue.c   4.4 (Berkeley) 6/1/90";
  3. static char rcsid[] = "$Id: db_glue.c,v 8.10 1995/12/22 10:20:30 vixie Exp $";
  4. #endif /* not lint */
  5.  
  6. /*
  7.  * ++Copyright++ 1986, 1988
  8.  * -
  9.  * Copyright (c) 1986, 1988
  10.  *    The Regents of the University of California.  All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *      This product includes software developed by the University of
  23.  *      California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  * -
  40.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41.  *
  42.  * Permission to use, copy, modify, and distribute this software for any
  43.  * purpose with or without fee is hereby granted, provided that the above
  44.  * copyright notice and this permission notice appear in all copies, and that
  45.  * the name of Digital Equipment Corporation not be used in advertising or
  46.  * publicity pertaining to distribution of the document or software without
  47.  * specific, written prior permission.
  48.  *
  49.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  52.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56.  * SOFTWARE.
  57.  * -
  58.  * --Copyright--
  59.  */
  60.  
  61. #include <sys/types.h>
  62. #include <sys/uio.h>
  63. #include <sys/param.h>
  64. #include <sys/stat.h>
  65. #include <netinet/in.h>
  66. #include <arpa/inet.h>
  67. #include <arpa/nameser.h>
  68. #include <stdio.h>
  69. #include <syslog.h>
  70. #include <ctype.h>
  71. #include <netdb.h>
  72. #include <resolv.h>
  73. #include <errno.h>
  74. #include <signal.h>
  75.  
  76. #include "named.h"
  77.  
  78. struct valuelist {
  79.         struct valuelist *next, *prev;
  80.         char    *name;
  81.         char    *proto;
  82.         int     port;
  83. };
  84. static struct valuelist *servicelist, *protolist;
  85.  
  86. #if defined(ultrix)
  87. /* ultrix 4.0 has some icky packaging details.  work around them here.
  88.  * since this module is linked into named and named-xfer, we end up
  89.  * forcing both to drag in our own res_send rather than ultrix's hesiod
  90.  * version of that.
  91.  */
  92. static const int (*unused_junk)__P((const u_char *, int, u_char *, int)) =
  93.         res_send;
  94. ;
  95. #endif
  96.  
  97. /*XXX: sin_ntoa() should probably be in libc*/
  98. const char *
  99. sin_ntoa(sin)
  100.         const struct sockaddr_in *sin;
  101. {
  102.         static char ret[sizeof "[111.222.333.444].55555"];
  103.  
  104.         if (!sin)
  105.                 strcpy(ret, "[sin_ntoa(NULL)]");
  106.         else
  107.                 sprintf(ret, "[%s].%u",
  108.                         inet_ntoa(sin->sin_addr),
  109.                         ntohs(sin->sin_port));
  110.         return (ret);
  111. }
  112.  
  113. /*
  114.  * XXX: some day we'll make this a varargs function
  115.  */
  116. void
  117. panic(err, msg)
  118.         int err;
  119.         const char *msg;
  120. {
  121.         if (err == -1)
  122.                 syslog(LOG_CRIT, "%s - ABORT", msg);
  123.         else
  124.                 syslog(LOG_CRIT, "%s: %s - ABORT", msg, strerror(err));
  125.         signal(SIGIOT, SIG_DFL);        /* no POSIX needed here. */
  126.         abort();
  127. }
  128.  
  129. void
  130. buildservicelist()
  131. {
  132.         struct servent *sp;
  133.         struct valuelist *slp;
  134.  
  135. #ifdef MAYBE_HESIOD
  136.         setservent(0);
  137. #else
  138.         setservent(1);
  139. #endif
  140.         while (sp = getservent()) {
  141.                 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
  142.                 if (!slp)
  143.                         panic(errno, "malloc(servent)");
  144.                 slp->name = savestr(sp->s_name);
  145.                 slp->proto = savestr(sp->s_proto);
  146.                 slp->port = ntohs((u_int16_t)sp->s_port);  /* host byt order */
  147.                 slp->next = servicelist;
  148.                 slp->prev = NULL;
  149.                 if (servicelist)
  150.                         servicelist->prev = slp;
  151.                 servicelist = slp;
  152.         }
  153.         endservent();
  154. }
  155.  
  156. void
  157. buildprotolist()
  158. {
  159.         struct protoent *pp;
  160.         struct valuelist *slp;
  161.  
  162. #ifdef MAYBE_HESIOD
  163.         setprotoent(0);
  164. #else
  165.         setprotoent(1);
  166. #endif
  167.         while (pp = getprotoent()) {
  168.                 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
  169.                 if (!slp)
  170.                         panic(errno, "malloc(protoent)");
  171.                 slp->name = savestr(pp->p_name);
  172.                 slp->port = pp->p_proto;        /* host byte order */
  173.                 slp->next = protolist;
  174.                 slp->prev = NULL;
  175.                 if (protolist)
  176.                         protolist->prev = slp;
  177.                 protolist = slp;
  178.         }
  179.         endprotoent();
  180. }
  181.  
  182. static int
  183. findservice(s, list)
  184.         register char *s;
  185.         register struct valuelist **list;
  186. {
  187.         register struct valuelist *lp = *list;
  188.         int n;
  189.  
  190.         for (; lp != NULL; lp = lp->next)
  191.                 if (strcasecmp(lp->name, s) == 0) {
  192.                         if (lp != *list) {
  193.                                 lp->prev->next = lp->next;
  194.                                 if (lp->next)
  195.                                         lp->next->prev = lp->prev;
  196.                                 (*list)->prev = lp;
  197.                                 lp->next = *list;
  198.                                 *list = lp;
  199.                         }
  200.                         return (lp->port);      /* host byte order */
  201.                 }
  202.         if (sscanf(s, "%d", &n) != 1 || n <= 0)
  203.                 n = -1;
  204.         return (n);
  205. }
  206.  
  207. /*
  208.  * Convert service name or (ascii) number to int.
  209.  */
  210. int
  211. servicenumber(p)
  212.         char *p;
  213. {
  214.         return (findservice(p, &servicelist));
  215. }
  216.  
  217. /*
  218.  * Convert protocol name or (ascii) number to int.
  219.  */
  220. int
  221. protocolnumber(p)
  222.         char *p;
  223. {
  224.         return (findservice(p, &protolist));
  225. }
  226.  
  227. #if defined(__STDC__) || defined(__GNUC__)
  228. static struct servent *
  229. cgetservbyport(u_int16_t port,          /* net byte order */
  230.                char *proto)
  231. #else
  232. static struct servent *
  233. cgetservbyport(port, proto)
  234.         u_int16_t port;                 /* net byte order */
  235.         char *proto;
  236. #endif
  237. {
  238.         register struct valuelist **list = &servicelist;
  239.         register struct valuelist *lp = *list;
  240.         static struct servent serv;
  241.  
  242.         port = ntohs(port);
  243.         for (; lp != NULL; lp = lp->next) {
  244.                 if (port != (u_int16_t)lp->port)